package org.artifact.core.lang;

import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.StrUtil;

import java.util.Map;

public class MapPlus {
    public static void merge(Map source,Map target){
        target.forEach((k,v) -> {
            Object sourceValue = source.get(k);
            if (sourceValue != null){
                if (sourceValue instanceof Map && v instanceof Map){
                    merge((Map) sourceValue,(Map) v);
                } else {
                    source.put(k,v);
                }
            } else {
                source.put(k,v);
            }
        });
    }

    public static <T> T get(String key,Map map){
        String[] keys = StrUtil.splitToArray(key, CharUtil.DOT,2);
        Object object = null;
        if (keys.length > 0){
            object = map.get(keys[0]);
        }
        if (keys.length > 1){
            if (object!=null && object instanceof Map){
                return get(keys[1],(Map) object);
            }
        }
        return (T) object;

//        String[] keys = key.split("\\.");
//        Object configMap = map;
//        for (String s : keys) {
//            if (configMap instanceof Map) {
//                configMap = ((Map<String, Object>) configMap).get(s);
//            } else {
//                break;
//            }
//        }
//        return (T) configMap;
    }
}
